/*
 * BanBu_WaveView
 * @description 本文件提供个人空间飘动的大背景
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供个人空间飘动的大背景
 */

#import "BanBu_WaveView.h"
#import "UIImageView+WebCache.h"

#define OFFSET_X 60
#define OFFSET_Y OFFSET_X
#define TIMEMAX_ONE 10   //移动一次的最长时间  来*speed 计算每次移动需要的时间
@implementation BanBu_WaveView

+(BanBu_WaveView *)instanceView
{
   
    return [[[NSBundle mainBundle] loadNibNamed:@"BanBu_WaveView" owner:nil options:nil] objectAtIndex:0];
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
    }
    return self;
}

- (void)selectImage:(UITapGestureRecognizer *)_tap
{
    if(_waveViewTap)
    {
        _waveViewTap();
    }
}
-(void)setImage:(UIImage *)image
{
    if(image)
    {
        [_imageBg setImage:image];
        [self startWave];
    }
}

- (void)setImageURL:(NSString *)imageURL
{
    _imageURL = [imageURL copy];
    
    if(_imageURL)
    {
  
        
        [_imageBg setImageWithURL:[NSURL URLWithString:_imageURL] placeholderImage:[UIImage imageNamed:@"wavedefault.jpg"] andType:1];
        [self startWave];
    }

//        [_imageBg setImageWithURL: placeholderImage:[UIImage imageNamed:@"logo.png"] success:^(UIImage *image) {
//            [self startWave];
//        } failure:^(NSError *error) {
//            
//        }];
        
//    }


    
}
- (void)setCanEdit:(BOOL)_edit
{
    canEdit = _edit;
    if(canEdit)
    {
        if(!tap)
        {
            tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectImage:)];
            [self addGestureRecognizer:tap];
            [tap release];
        }
    }
    
}
- (void)startWave
{
    [self onTimer];
    speed = 40 / round(random() % 100);
    if(!_timer)
    {
        _timer = [NSTimer scheduledTimerWithTimeInterval:TIMEMAX_ONE target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
     }
}

/** 320 320  480 480
 *	@brief	frame x (-60 - 0) y (-60 - 0) 方向 4个边角 以随机数产生0123 上左、上右、下左、下右
 
 */
- (void)onTimer
{
    // use the random() function to randomize up our flake attributes
    NSInteger offsetX = OFFSET_X + arc4random()%60 - 30;
    NSInteger offsetY = offsetX;
    
    NSArray *frames = @[NSStringFromCGRect(CGRectMake(-offsetX, -offsetY,offsetX+320,offsetY+320)),NSStringFromCGRect(CGRectMake(0, -offsetY,offsetX+320,offsetY+320)),NSStringFromCGRect(CGRectMake(-offsetX, 0,offsetX+320,offsetY+320)),NSStringFromCGRect(CGRectMake(0,0,offsetX+320,offsetY+320))];

    int direct = arc4random()%4;
    
    NSString *cp = NSStringFromCGRect(self.imageBg.frame);
    while ([[frames objectAtIndex:direct] isEqualToString:cp])
    {
        direct = arc4random()%4;
    }

    

    [UIView beginAnimations:nil context:self.imageBg];
    [UIView setAnimationDuration:TIMEMAX_ONE];
    
    self.imageBg.frame = CGRectFromString([frames objectAtIndex:direct]);
    NSLog(@"%@",[frames objectAtIndex:direct]);
    [UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
}

- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
}

/**
 *	@brief	释放本view必须先执行此方法 放在dealloc里面release之前
 */
-(void)destoryTimerMove
{
    [_timer invalidate];
    _timer = nil;
}

- (void)dealloc
{
    self.imageURL = nil;
    [_imageBg release];
    [super dealloc];
}
@end
